var app = getApp();

Page({

    data: {
        is_default: '/images/q.jpg',
        indexs: '',
        region: ['广东省', '广州市', '海珠区'],
    },
    bindRegionChange: function (e) {
      this.setData({
        region: e.detail.value
      })
    },
    onLoad: function(e) {
      if(e.etype == '1'){
        this.getAddr(e)
      }
      this.setData(e)
    },
    // 获取地址信息
    getAddr:function(e){
      var that= this 
      wx.showLoading({
        title: '正在加载',
      })
      wx.request({
        url: app.globalData.url+ 'addr?id='+e.id,
        success:function(res){
          console.log(res)
          var addr = res.data[0]
          that.setData({
            name: addr.name,
            tel:addr.tel,
            xx: addr.xiangxi,
            region: [addr.sheng, addr.shi,addr.qu]
          })
        },
        complete:()=>{
          wx.hideLoading()
        }
      })
    },
    onEdit:function(){
      if (this.data.is_default == '/images/q.jpg') {
        this.setData({
          default_code: 0
        })
      } else if (this.data.is_default == '/images/d.jpg') {
        this.setData({
          default_code: 1
        })
      }

      var openId = wx.getStorageSync('openId');
      var name = this.data.name;
      var tel = this.data.tel;
      var region = this.data.region
      var xx = this.data.xx
      var id = this.data.id
      var default_code = this.data.default_code; //是否默认

      if (!openId || typeof (name) == 'undefined' || typeof (tel) == 'undefined') {
        wx.showToast({
          title: '请完善信息',
        })
      } else {
        wx.request({
          url: app.globalData.url + 'addr_update?id=' +
          id + '&name=' + name + '&tel=' + tel + '&sheng=' + region[0] + '&shi=' + region[1] + '&qu=' + region[2] + '&xiangxi=' + xx,

          success: function (res) {
            console.log(res.data);
            
            if (typeof res.data == 'number') {
              wx.showToast({
                title: '修改成功',
                icon: 'success',
                duration:1500,
                mask: true,
              })
              wx.navigateBack({
                delta: 1,
              })
            }
          }
        })
      }
    },
    //姓名
    onName: function(e) {
        var name = e.detail.value;
        if (this.validate(name)) return false
        this.setData({
            name: name
        })
    },

    //电话
    onTel: function(e) {
        var tel = e.detail.value;
        if (this.validate(tel)) return false
        this.setData({
            tel: tel
        })
    },

    //详细地址
    onXx: function(e) {
        var xx = e.detail.value;
        if (this.validate(xx)) return false
        this.setData({
            xx: xx
        })
    },

    //设为默认
    select1: function(e) {
        if (this.data.is_default == '/images/q.jpg') {
            this.setData({
                is_default: '/images/d.jpg',
                default_code: 1
            })
        } else if (this.data.is_default == '/images/d.jpg') {
            this.setData({
                is_default: '/images/q.jpg',
                default_code: 0
            })
        }

    },
    validate: function(str) {
        if (str == null) return true;
        if (typeof(str) == 'undefined') return true
        if (typeof str == 'string') {
            if (str == "") return true;
            let reg = new RegExp("^([ ]+)|([　]+)$")
            return reg.test(str)
        }
        return false
    },
    //确认添加
    onSubmit: function(e) {

        if (this.data.is_default == '/images/q.jpg') {
            this.setData({
                default_code: 0
            })
        } else if (this.data.is_default == '/images/d.jpg') {
            this.setData({
                default_code: 1
            })
        }

        var openId = wx.getStorageSync('openId');
        var name = this.data.name;
        var tel = this.data.tel;
        var region = this.data.region
        var xx = this.data.xx
        var default_code = this.data.default_code; //是否默认

        if ( !openId || typeof(name) == 'undefined' || typeof(tel) == 'undefined') {
            wx.showToast({
                title: '请完善信息',
            })
        } else {
            wx.request({
              url: app.globalData.url + 'addr_add?openid=' +
              openId + '&name=' + name + '&tel=' + tel + '&sheng=' + region[0] + '&shi=' + region[1] + '&qu=' + region[2] +'&xiangxi='+xx,

                success: function(res) {
                    console.log(res.data);
                    if (typeof res.data == 'number') {
                      wx.showToast({
                        title: '修改成功',
                        icon: 'success',
                        duration: 1500,
                        mask: true,
                      })
                      wx.navigateBack({
                        delta: 1,
                      })
                    }
                }
            })
        }
    }
})